var gulp= require('gulp');
var less= require('gulp-less');
var cssnano= require('gulp-cssnano');
var concat= require('gulp-concat');
var uglify= require('gulp-uglify');
var htmlmin= require('gulp-htmlmin');
var browserSync= require('browser-sync').create();
var reload= browserSync.reload;
// less编译压缩合并
gulp.task('style', function () {
//    这里是在执行style任务时候自动执行
    gulp.src(['src/styles/*.less', '!src/styles/_*.less'])
        .pipe(less())
        .pipe(cssnano())
        .pipe(gulp.dest('dist/styles'))
        .pipe(reload({
            stream: true
        }));

});
// JS合并压缩混淆
gulp.task('script', function () {
    gulp.src('src/js/*.js')
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js/'))
        .pipe(reload({
            stream: true
        }));

});
//img复制
gulp.task('image', function () {
    gulp.src('src/images/*.*')
        .pipe(gulp.dest('dist/images'))
        .pipe(reload({
            stream: true
        }));

});
//HTML压缩
gulp.task('html', function () {
    gulp.src('src/*.html')
        .pipe(htmlmin({
            collapseWhitespace: true,
            removeComments: true,
            removeAttributeQuotes: true
        }))
        .pipe(gulp.dest('dist'))
        .pipe(reload({
            stream: true
        }));
});
//启动一个服务， 顺便监视文件的变化
gulp.task('serve', function () {
    browserSync.init({
        server: {
            baseDir: 'dist/'
        }
    }, function(err, bs) {
        console.log(bs.options.getIn(["urls", "local"]));
    });
    gulp.watch('src/styles/*.less',['style']);
    gulp.watch('src/js/*.js', ['script']);
    gulp.watch('src/images/*.*', ['image']);
    gulp.watch('src/*.html', ['html']);
});
